﻿#include "mainwindow.h"

#include <QApplication>
#include <QDir>
#include <QFontDatabase>
#include <QMessageBox>
#include <QSharedMemory>
#include <QTranslator>
#include <QtDebug>
#include "model/datamanager.h"
#include "common/loghandler.h"
#include "common/common.h"
void loadFont()
{
    // 定义一个包含所有字体文件资源路径的列表
    QStringList fontFiles;
    QString path = QDir::currentPath();
    fontFiles << path + "/MiSans/otf/MiSans-Regular.otf";
    // fontFiles << path + "/MiSans/otf/MiSans-Bold.otf"
    //           << path + "/MiSans/otf/MiSans-Demibold.otf"
    //           << path + "/MiSans/otf/MiSans-ExtraLight.otf"
    //           << path + "/MiSans/otf/MiSans-Heavy.otf"
    //           << path + "/MiSans/otf/MiSans-Light.otf"
    //           << path + "/MiSans/otf/MiSans-Medium.otf"
    //           << path + "/MiSans/otf/MiSans-Normal.otf"
    //           << path + "/MiSans/otf/MiSans-Regular.otf"
    //           << path + "/MiSans/otf/MiSans-Semibold.otf"
    //           << path + "/MiSans/otf/MiSans-Thin.otf";

    int loadedCount = 0;
    for (const QString &fontFile : fontFiles) {
        int fontId = QFontDatabase::addApplicationFont(fontFile);
        if (fontId == -1) {
            qWarning() << "Failed to load font:" << fontFile;
        } else {
            loadedCount++;
        }
    }
    if (loadedCount > 0) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(0);
        if (!fontFamilies.isEmpty()) {
            qDebug() << "Successfully loaded" << loadedCount << "weights for font family:" << fontFamilies.first();
        }
    }
}

static QSharedMemory *g_pSharedMemory = nullptr;

void cleanupSharedMemory()
{
    if (g_pSharedMemory) {
        g_pSharedMemory->detach(); // 在进程退出前分离共享内存
        delete g_pSharedMemory;    // 然后删除对象
        g_pSharedMemory = nullptr;
        qDebug() << "Shared memory detached and cleaned up.";
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setApplicationName("EMC Custom Software");
    a.setApplicationVersion(APP_VERSION);
    a.setOrganizationName("Halox");
    a.setOrganizationDomain("halox.cn");

    //通过共享内存来实现只有一个实例启动.
    g_pSharedMemory = new QSharedMemory("EMCCustom");
    if (g_pSharedMemory->create(1) && g_pSharedMemory->error() != QSharedMemory::AlreadyExists) {
        QObject::connect(&a, &QApplication::aboutToQuit, cleanupSharedMemory);
        QTranslator translate;
        QString language = Utils::getAppLanguage();
        if (language == "en_US") {
            translate.load("./translate/en.qm");
        } else {
            translate.load("./translate/ch.qm");
        }

        qApp->installTranslator(&translate);
        LogHandler::install();
        DataManager::instance()->initialize();
        loadFont();
        MainWindow w;
        w.show();
        return a.exec();
    } else {
        QMessageBox::critical(NULL, "提示", "程序正在运行!");
        delete g_pSharedMemory;
        g_pSharedMemory = nullptr;
        return -1;
    }

    return a.exec();
}
